import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../components/Home.vue";
import api from "../api/index";
import storage from "../utils/index";
import store from "../store";
const routes = [
  {
    path: "/",
    name: "home",
    component: Home,
    redirect: "/Welcome",
    meta: {
      title: "首页",
      isLogin: true,
    },
    children: [
      {
        path: "Welcome",
        name: "Welcome",
        meta: {
          title: "欢迎页",
          isLogin: true,
        },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/Welcome.vue"),
      },
      {
        path: "menu",
        name: "menu",
        meta: {
          title: "菜单页",
          isLogin: true,
        },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/Menu.vue"),
      },
      // {
      //     path:'User',
      //     name:'User',
      //     meta:{
      //         title:"用户页",
      //         isLogin:true
      //     },
      //     component: () =>
      //     import(/* webpackChunkName: "about" */ "../views/User.vue"),
      // },

      // {
      //     path:'department',
      //     name:'department',
      //     meta:{
      //         title:"部门页",
      //         isLogin:true
      //     },
      //     component: () =>
      //     import(/* webpackChunkName: "about" */ "../views/department.vue"),
      // },
      // {
      //     path:'role',
      //     name:'role',
      //     meta:{
      //         title:"角色页",
      //         isLogin:true
      //     },
      //     component: () =>
      //     import(/* webpackChunkName: "about" */ "../views/role.vue"),
      // },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/login.vue"),
    meta: {
    
      title:"登陆"
    },
  },
  {
    path: "/404",
    name: "404",
    component: () => import(/* webpackChunkName: "about" */ "../views/404.vue"),
    meta: {
    
      title:"404"
    },
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

//新增路由对象到routes

//建议异步路由解析规则
const genearateRoute = (menulist) => {
  //  通过递归解析出二级菜单 并形成一个对象
  let arr = [];

  const deepList = (list) => {
    while (list.length) {
      let item = list.pop();
        
      if (item.action) {
        //二级菜单
        arr.push({
          name: item.component,
          path: item.path,
          meta: { title: item.menuName },
        });
      }
      if (!item.action && item.children) {
        deepList(item.children);
      }
    }
  };
  deepList(menulist);

  return arr;
};

//获取异步路由

const asnuyLoadRoute = async () => {
  let userInfo = storage.getSession("userInfo") || {};

  if (userInfo.data.token) {
    let menuList = await (await api.getPermisssonList()).data.data.menuList;
   
    let asnycRoutes = genearateRoute(menuList);
   
    asnycRoutes.map((ele) => {
      //获取views路径下所有的vue文件
      const moudle = import.meta.glob("../views/*.vue");
      let url = `../views/${ele.name}.vue`;
      ele.component = moudle[url];
      router.addRoute("home", ele);  
    });
  }
};

//判断是否有权限跳转至该路由



router.beforeEach(async(to, from, next) => {
  // if(to.meta.isLogin){
  let result = storage.getSession("userInfo");
 
    if (to.name) {

      next()
      //修改标题
      document.title=to.meta.title
    } else {
        await asnuyLoadRoute()
        //判断是否有权限跳转至该路由
        let flag=router.getRoutes().some((ele) => ele.path == to.path);
    
        if (flag) {
            document.title=to.meta.title
            next(to);
        }else{
            next("/404");
        }
        
     }
 
  
});
export default router;
